package cn.wolfcode.luowowo.acticle.service.impl;


import cn.wolfcode.luowowo.acticle.domain.*;
import cn.wolfcode.luowowo.acticle.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.acticle.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.acticle.mapper.StrategyMapper;
import cn.wolfcode.luowowo.acticle.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.acticle.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Transactional
@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyTagMapper strategyTagMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;


    @Reference
    private IDestinationService destinationService;

    @Autowired
    private StrategyMapper strategyMapper;


    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public StrategyDetail get(Long id) {



        return strategyDetailMapper.selectByPrimaryKey(id);
    }



    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {

        //目的地
        Strategy strategy = strategyMapper.selectByPrimaryKey(strategyDetail.getStrategy().getId());
        Destination destination = strategy.getDest();
        strategyDetail.setDest(destination);

        //作者id
        strategyDetail.setAuthor(null); //当前登录对象（管理员）

        //文章摘要
        if(strategyDetail.getStrategyContent().getContent().length() < 200){
            strategyDetail.setSummary(strategyDetail.getStrategyContent().getContent());
        }else{
            String summary = strategyDetail.getStrategyContent().getContent().substring(0, 200);
            strategyDetail.setSummary(summary);
        }

        //是否国外
        strategyDetail.setIsabroad(destinationService.isAbroad(destination.getId()));

        if(strategyDetail.getId() == null){
            //创建时间
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);

            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);

            //主题攻略推荐
            //ZSetOperations zset = redisTemplate.opsForZSet();
            //zset.incrementScore(RedisKeyConstant.THEME_STRATEGY_SORT_ZSET, strategyDetail.getTheme().getId(), 1L);

        }else{

            //StrategyDetail oldDetail = strategyDetailMapper.selectByPrimaryKey(strategyDetail.getId());
            strategyTagMapper.deleteRelation(strategyDetail.getId());
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);



        }

        //处理标签
        if(tags != null){
            String[] split = tags.split(",");
            for (String tag : split) {
                if(StringUtils.isEmpty(tag)){
                    continue;
                }
                //已经存在，不需要添加
                List<StrategyTag> ts = strategyTagMapper.selectTagByName(tag);
                StrategyTag t = null;
                if(ts == null || ts.size() == 0){

                    t = new StrategyTag();
                    t.setName(tag);
                    t.setState(StrategyTag.STATE_NORMAL);
                    strategyTagMapper.insert(t);
                }else{
                    t = ts.get(0);
                }
                strategyTagMapper.insertRelation(t.getId(), strategyDetail.getId());
            }
        }



    }


    @Override
    public void changeState(Long id, int state) {
        strategyDetailMapper.updateState(id, state);
    }

    @Override
    public String getTags(Long id) {
        return strategyDetailMapper.selectTagsByDetailId(id);
    }

    @Override
    public List<Map<String, Object>> getDetailByStrategyId(Long strategyId) {
        return strategyDetailMapper.selectDetailByStrategyId(strategyId);
    }

    @Override
    public List<StrategyDetail> queryStrategyDetailByDestId(Long id) {
        StrategyDetailQuery qo = new StrategyDetailQuery();
        qo.setDestId(id);
        PageHelper.startPage(qo.getCurrentPage(), 3, "sd.clicknum desc");

        return strategyDetailMapper.selectForList(qo);
    }





    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }


    @Override
    public List<Long> getUserFavorList(Long sid) {
        return strategyDetailMapper.selectUserFavorList(sid);
    }

    @Override
    public void updateStatis(StrategyStatisVO vo) {
        strategyDetailMapper.updateStatis(vo);
    }

    @Override
    public void deleteFavor(Long id) {
        strategyDetailMapper.deleteFavor(id);
    }

    @Override
    public void insertFavor(Long sid, Long uid) {
        strategyDetailMapper.insertFavor(sid, uid);
    }

    public List<Map<String, Object>> getDetails() {
        return strategyDetailMapper.selectDetails();
    }
}
